module test

    contains

    subroutine vec_mult(p, v1, v2, N)
       real    ::  p(N), v1(N), v2(N)
       integer ::  i,j

       call init(v1, v2, N)

      p = 0
      !$omp target data device(2) map(to:v1,v2) map(from:p)
         !$omp target device(1)
            do i=1,N
               p(i) = v1(i) * v2(i)
            end do
         !$omp end target
      !$omp end target data
      if(p(1)/=0)then
         write(*,*)"p0 = ",p(1)
      endif

      p = 0
      !$omp target data device(2) map(to:v1,v2) map(from:p)
         !$omp target device(2)
            do i=1,N
               p(i) = v1(i) * v2(i)
            end do
         !$omp end target
      !$omp end target data
      if(p(1)/=6.0)then
         write(*,*)"p1 = ",p(1)
      endif


      p = 0
      !$omp target data device(2) map(to:v1,v2) map(from:p)
         !$omp target device(2)
            do i=1,N
               p(i) = v1(i) * v2(i)
            end do
         !$omp end target
         !$omp target update from(p) device(2)
      !$omp end target data
      if(p(1)/=6.0)then
         write(*,*)"p2 = ",p(1)
      endif

      p = 0
      !$omp target data device(2) map(to:v1,v2) map(from:p)
         !$omp target device(2)
            do i=1,N
               p(i) = v1(i) * v2(i)
            end do
         !$omp end target
         !$omp target update from(p) device(2)
         if(p(1)/=6.0)then
            write(*,*)"p3 = ",p(1)
         endif
      !$omp end target data

    end subroutine

    subroutine init(v1, v2, N)
       integer,intent(in)::N
       real,intent(inout)::v1(N), v2(N)

       v1 = 2.

       v2 = 3.
    end subroutine


    end module


    program main
    use test
       integer, parameter :: N=1024
       real    ::  p(N), v1(N), v2(N)

       call vec_mult(p, v1, v2, N)


    end program

